package com.example.xiazai;

import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.xiazai.adapter.VpTabAdapter;
import com.example.xiazai.fragment.HomeFragment;
import com.example.xiazai.fragment.XiaZaiFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.vp_tab)
    ViewPager mVpTab;
    @BindView(R.id.tab)
    TabLayout mTab;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        ArrayList<Fragment> fragments = new ArrayList<>();
        HomeFragment homeFragment = new HomeFragment();
        XiaZaiFragment xiaZaiFragment = new XiaZaiFragment();
        fragments.add(homeFragment);
        fragments.add(xiaZaiFragment);
        VpTabAdapter vpTabAdapter = new VpTabAdapter(getSupportFragmentManager(), fragments);
        mVpTab.setAdapter(vpTabAdapter);
        mTab.setupWithViewPager(mVpTab);
        mTab.getTabAt(0).setText("首页").setIcon(R.drawable.tab1);
        mTab.getTabAt(1).setText("下载").setIcon(R.drawable.tab2);
        mTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mTvTitle.setText(tab.getText());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
}